package com.tpc.wdt.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tpc.wdt.WdtClient;
import com.tpc.wdt.utils.JDBCUtil;
import com.tpc.wdt.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class JoinInPush {

    @Value("${wdt.sid}")
    private String sid;

    @Value("${wdt.appkey}")
    private String appkey;

    @Value("${wdt.appsecret}")
    private String appsecret;

    @Value("${wdt.url}")
    private String url;

    @Value("${wdt.joinin.sql}")
    private String joinInSql;

    public void joinInPush(){
        WdtClient client = new WdtClient(sid, appkey, appsecret, url);

        Connection connection = JDBCUtil.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String isUpload = "";
        String uploadInfo = "";
        try {
            Map<String, Object> result = queryRunner.query(connection, joinInSql, new MapHandler());
            String valueInfo = JSON.toJSONString(result);
            if(valueInfo.equals("null")) {
                return;
            }
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(JSONUtil.transToLowerObject(valueInfo));
            Map<String, Object> stockin_info = new HashMap<String, Object>();
            List<Map<String, Object>> goods_list = new ArrayList<Map<String, Object>>();
            Map<String, Object> goods_1 = new HashMap<String, Object>();
            goods_1.put("spec_no", jsonObject.getString("spec_no"));
            goods_1.put("stockin_num", jsonObject.getString("stockin_num"));
            goods_1.put("batch_no", jsonObject.getString("batch_no"));
            goods_1.put("production_date", jsonObject.getString("production_date"));
            goods_1.put("expire_date", jsonObject.getString("expire_date"));
            goods_1.put("validity_days", jsonObject.getString("validity_days"));
            goods_1.put("src_price", jsonObject.getString("src_price"));
            goods_1.put("stockin_price", jsonObject.getString("stockin_price"));
            goods_1.put("tax", jsonObject.getString("tax"));
            goods_list.add(goods_1);
            stockin_info.put("outer_no", jsonObject.getString("outer_no"));
            stockin_info.put("warehouse_no", jsonObject.getString("warehouse_no"));
            stockin_info.put("is_check", jsonObject.getString("is_check"));
            stockin_info.put("remark", jsonObject.getString("remark"));
            stockin_info.put("is_create_batch", jsonObject.getString("is_create_batch"));

            stockin_info.put("goods_list", goods_list);

            String stockin_info_json = JSON.toJSONString(stockin_info);

            Map<String, String> params = new HashMap<String, String>();
            params.put("stockin_info", stockin_info_json);
            try {
                log.info("其他入库请求信息：{}",JSONObject.toJSONString(params));
                String response = client.execute("stockin_order_push.php", params);
                log.info("其他入库商品返回信息：{}",response);
                JSONObject jsonResponse = JSONObject.parseObject(response);
                uploadInfo = jsonResponse.getString("message");
                if(jsonResponse.getString("stockin_no")!=null){
                    uploadInfo = uploadInfo + jsonResponse.getString("stockin_no");
                }
                if(jsonResponse.getInteger("code")==0){
                    isUpload = "Y";
                }else{
                    isUpload = "N";
                }
                if(jsonResponse.getInteger("code")==2470){
                    isUpload = "Y";
                }
                StringBuilder updateSql = new StringBuilder();
                if(jsonObject.getString("bs").equals("QC")){
                    updateSql.append("update WDT_STOCK_DOC_20250530 set IS_WDT_NEW = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where OUTER_NO = '" + jsonObject.getString("outer_no") +"'") ;
                }else if(jsonObject.getString("bs").equals("RK")) {
                    updateSql.append("update joinindt set IS_WDT_NEW = '" + isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'RK-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") + "'");
                }else if(jsonObject.getString("bs").equals("XTTR")) {
                    updateSql.append("update trgoodsdt set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'XT-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                }else if(jsonObject.getString("bs").equals("XTRET")) {
                    updateSql.append("update retgoodsdt set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'XT-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                } else if (jsonObject.getString("bs").equals("DB")) {
                    updateSql.append("update MOVEINDT set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'DB-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                } else if (jsonObject.getString("bs").equals("BY")) {
                    updateSql.append("update ALEXECDT set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'BY-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                } else if (jsonObject.getString("bs").equals("CL")) {
                    updateSql.append("update GOODSDESTDT set is_wdt_new = '" +isUpload + "',WDT_INFO = '" + uploadInfo + "' where 'CL-'||billno ||'-'||billsn = '" + jsonObject.getString("outer_no") +"'");
                }
                queryRunner.update(connection,updateSql.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection);
        }
    }
}
